package com.snpic.appaw.module.third.service.deepOCR;

import com.alibaba.fastjson.JSON;
import com.snpic.appaw.framework.common.exception.ServiceException;
import com.snpic.appaw.module.third.api.deepOCR.dto.IdCardReqDTO;
import com.snpic.appaw.module.third.api.deepOCR.dto.IdCardRespDTO;
import com.snpic.appaw.module.third.api.deepOCR.dto.VehicleLicenseReqDTO;
import com.snpic.appaw.module.third.api.deepOCR.dto.VehicleLicenseRespDTO;
import com.snpic.appaw.module.third.api.jpush.dto.LoginTokenVerifyRespDTO;
import com.snpic.appaw.module.third.api.sms.dto.SmsReqDTO;
import com.snpic.appaw.module.third.config.DeepOCRConfig;
import com.snpic.appaw.module.third.service.httpclient.HttpClientServerImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import static com.snpic.appaw.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.snpic.appaw.module.third.enums.ErrorCodeConstants.*;

/**
 * @author JiJun
 * @date 2025/9/11 10:53
 * @修改时间 2025/9/11 10:53
 * @description
 */
@Service
@Slf4j
public class DeepOCRServiceImpl implements DeepOCRService {
	@Resource
	private DeepOCRConfig deepOCRConfig;

	@Resource
	private HttpClientServerImpl httpClientServer;

	/**
	 * 行驶证(正页识别)
	 *
	 * @param reqDTO
	 * @return
	 */
	@Override
	public VehicleLicenseRespDTO vehicleLicense(VehicleLicenseReqDTO reqDTO) {
		// 1. 查教第三方接口地址
		String url = deepOCRConfig.getVehicleLicense().getUrl();

		// 2.1构建请求头,content-type应该为application/x-www-form-urlencoded
		Map<String, String> headers = new HashMap<>();
		headers.put("Content-Type", "application/x-www-form-urlencoded");

		// 3. 调用第三方接口
		String response = null;
		try {
			response = httpClientServer.doPostJson(url, reqDTO, headers, "易道博识ORC", "行驶证正面识别");
		} catch (HttpClientServerImpl.HttpClientException e) {
			log.error("第三方接口调用失败,易道博识ORC,行驶证正面识别", e);
			throw exception(VEHICLE_LICENSE_FAILURE);
		}

		// 5. 解析返回结果
		VehicleLicenseRespDTO respDTO = JSON.parseObject(response, VehicleLicenseRespDTO.class);
		return respDTO;
	}

	@Override
	public void veBack(SmsReqDTO reqDTO) {

	}

	/**
	 * 易道博识DeepOCR,身份证结构化识别V1
	 * @param reqDTO
	 * @return
	 */
	@Override
	public IdCardRespDTO idCard(IdCardReqDTO reqDTO) {
		// 1. 查教第三方接口地址
		String url = deepOCRConfig.getIdCard().getUrl();

		// 2.1构建请求头,content-type应该为application/x-www-form-urlencoded
		Map<String, String> headers = new HashMap<>();
		headers.put("Content-Type", "application/x-www-form-urlencoded");

		// 3. 调用第三方接口
		String response = null;
		try {
			response = httpClientServer.doPostJson(url, reqDTO, headers, "易道博识ORC", "身份证结构化识别V1");
		} catch (HttpClientServerImpl.HttpClientException e) {
			log.error("第三方接口调用失败,易道博识ORC,身份证结构化识别V1", e);
			throw exception(ID_CARD_FAILURE);
		}

		// 5. 解析返回结果
		IdCardRespDTO respDTO = JSON.parseObject(response, IdCardRespDTO.class);
		return respDTO;

	}
}
